<template>
  <el-dialog title="条件判断"
             :visible.sync="visible"
             v-loading="loading"
             :before-close="modalClose"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="90%">
    <el-form :model="submitData"
             label-position="right"
             label-width="150px">
      <el-form-item label="条件列表:"></el-form-item>
      <el-form-item>
        <element-table :columns="columnsCondition"
                       :data="submitData.conditions">
          <template v-slot:index="{row,index}">
            <p>条件{{ index + 1 }}</p>
          </template>

          <template v-slot:genderType="{row}">
            <el-select v-model="row.genderType"
                       clearable
                       disabled
                       placeholder="选择性别">
              <el-option v-for="item in sexList"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </template>

          <template v-slot:relatedDiseaseId="{row}">
            <el-select v-model="row.relatedDiseaseId"
                       clearable
                       disabled
                       placeholder="关联病种">
              <el-option v-for="item in bingzhongList"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </template>

          <template v-slot:unRelatedDiseaseId="{row}">
            <el-select v-model="row.unRelatedDiseaseId"
                       clearable
                       disabled
                       placeholder="未关联病种">
              <el-option v-for="item in bingzhongList"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </template>

          <template v-slot:firstCondition="{row}" >
            <el-select v-model="row.firstCondition"
                       clearable
                       v-if="row.indexResultType!=2"
                       disabled
                       style="width: 120px"
                       placeholder="关系">
              <el-option v-for="item in fuhaoLeft"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </template>


          <template v-slot:secondCondition="{row}">
            <el-select v-model="row.secondCondition"
                       clearable
                       style="width: 120px"
                       disabled
                       placeholder="关系">
              <el-option v-for="item in fuhaoRight"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </template>


        </element-table>
      </el-form-item>


      <el-form-item label="条件判断：" required>
        <el-button type="primary" @click="handleAddTiaojianStatus()">增 加</el-button>
      </el-form-item>

      <el-form-item v-for="(item,index) in judgesData.judges" :key="index+'b'">
        <el-select v-model="item.conditionIdList"
                   clearable
                   multiple
                   style="width: 360px"
                   placeholder="请选择条件">
          <el-option v-for="item in submitData.conditions"
                     :key="item.id"
                     :label="item.tiaojianName"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-select v-model="item.conditionType"
                   clearable
                   style="width: 120px"
                   placeholder="请选择满足条件">
          <el-option v-for="item in satisfyList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-select v-model="item.stateId"
                   clearable
                   style="width: 120px"
                   placeholder="请选择结论">
          <el-option v-for="item in submitData.targetStates1"
                     :key="item.id"
                     :label="item.stateName"
                     :value="item.id">
          </el-option>
        </el-select>

        <el-button type="danger" @click="handleDelTiaojianStatus(index)">删 除</el-button>
      </el-form-item>
    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="submitTiaojianStatus()">保 存
      </el-button>
    </p>

  </el-dialog>

</template>

<script>
import ElementTable from '@/base/ElementTable'

export default {
  name: "judgeDialog",
  props: {
    eventdata: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      submitData: {conditions: [], targetStates1: []},
      columnsCondition: [
        {
          label: '条件编号',
          prop: 'index',
          align: 'center',
          isSlot: true
        }, {
          label: '性别',
          prop: 'genderType',
          align: 'center',
          isSlot: true,
          width: 150
        }, {
          label: '量表',
          prop: 'scaleName',
          align: 'center',
          width: 150
        }, {
          label: '关联病种',
          prop: 'relatedDiseaseId',
          align: 'center',
          isSlot: true,
          width: 150
        }, {
          label: '未关联病种',
          prop: 'unRelatedDiseaseId',
          align: 'center',
          isSlot: true,
          width: 150
        }, {
          label: '最小年龄',
          prop: 'minAge',
          align: 'center',
        }, {
          label: '最大年龄',
          prop: 'maxAge',
          align: 'center',
        }, {
          label: '左边值',
          prop: 'firstValue',
          align: 'center'
        }, {
          label: '关系',
          prop: 'firstCondition',
          align: 'center',
          isSlot: true,
          width: 150
        }, {
          label: '指标',
          prop: 'indexName',
          align: 'center',
          width: 150,
          formatter: (row, cellValue) => {
            return this.commonFunction.delUndefine(row.indexName + this.commonFunction.delUndefine(row.indexUnit))
          }
        }, {
          label: '关系',
          prop: 'secondCondition',
          align: 'center',
          isSlot: true,
          width: 150
        }, {
          label: '右边值',
          prop: 'secondValue',
          align: 'center',
        }, {
          label: '检测项类型',
          prop: 'qualitative',
          align: 'center',
          formatter: (row, cellValue) => {
            if (row.indexResultType==2){
              switch (row.firstValue) {
                case 1:
                  return '阴性'
                  break
                case 2:
                  return '弱阴性'
                  break
                case 3:
                  return '弱阳性'
                  break
                case 4:
                  return '阳性'
                  break
              }

            }else if (row.indexResultType==1){
              return '定量'
            }
          }
        }
      ],
      sexList: [
        {
          name: '男女一致',
          id: 1
        }, {
          name: '男',
          id: 2
        }, {
          name: '女',
          id: 3
        }],
      bingzhongList: [],
      fuhaoRight: [
        {
          name: '<',
          id: 4
        }, {
          name: '<=',
          id: 5
        }
      ],
      fuhaoLeft: [
        {
          name: '=',
          id: 1
        }, {
          name: '<',
          id: 4
        }, {
          name: '<=',
          id: 5
        }],
      satisfyList: [
        {
          name: '全部满足',
          id: 1
        }, {
          name: '任何一满足',
          id: 2
        }, {
          name: '满足任何两个',
          id: 3
        }],
      zhibiaoList: [],
      judgesData: {
        id: '',
        judges: []
      },
      resultData: {}
    }

  },
  created() {
    this.resultData = this.eventdata
    this.getHealthMapDiseaseList()
    this.getHealthMapIndexItemList()
    this.getHealthMapTargetDetail()
    this.getTargetConfigure()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    //增加条件满足项
    handleAddTiaojianStatus() {
      var list = this.judgesData.judges
      var data = {
        conditionIdList: [],
        conditionType: '',
        stateId: '',
      }
      list.push(data)
    },
    //删除条件满足项
    handleDelTiaojianStatus(index) {
      this.judgesData.judges.splice(index, 1)
    },
    //提交条件状态
    submitTiaojianStatus() {
      this.judgesData.id = this.resultData.childId
      this.getHealthMapTargetState()
    },
    getHealthMapTargetState() {
      this.loading = true
      this.submitData.diseaseId = this.resultData.diseaseId
      this.post(this.api.healthMap.getHealthMapTargetState, this.judgesData).then((res) => {
        this.loading = false
        if (res.data.code === '200') {
          this.$message.success('操作成功')
          this.modalClose()
          this.$parent.getHealthMapTargetList()
        }
      })
    },
    getHealthMapTargetDetail() {
      this.post(this.api.healthMap.getHealthMapTargetDetail, {
        id: this.resultData.childId
      }).then((res) => {
        if (res.data.code === '200') {
          var bean = res.data.object
          this.submitData.conditions = bean.conditions ? bean.conditions : []
          if (this.submitData.conditions) {
            this.submitData.conditions.forEach((item, index) => {
              this.submitData.conditions[index].tiaojianName = '条件' + (index + 1)
            })
          }
          this.submitData.targetStates1 = bean.targetStates

        }
      })
    },
    //条件判断回显
    getTargetConfigure() {
      this.post(this.api.healthMap.getTargetConfigure, {
        id: this.resultData.childId
      }).then((res) => {
        if (res.data.code === '200') {
          this.judgesData.judges = res.data.object
        }
      })
    },
    //查询病种
    getHealthMapDiseaseList() {
      this.post(this.api.healthMap.getHealthMapDiseaseExcludeList, {
        id: this.resultData.diseaseId
      }).then((res) => {
        if (res.data.code === '200') {
          this.bingzhongList = res.data.object
        }
      })
    },
    //查询指标
    getHealthMapIndexItemList() {
      this.post(this.api.healthMap.getHealthMapIndexItemList).then((res) => {
        if (res.data.code === '200') {
          this.zhibiaoList = res.data.object
        }
      })
    },
  },
  components: {
    'element-table': ElementTable
  }
}
</script>

<style scoped>

</style>
